package com.tycmc.signelectroniccontracts.ui;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.tycmc.signelectroniccontracts.R;
import com.tycmc.signelectroniccontracts.entity.QianYueEntity;
import com.tycmc.signelectroniccontracts.tools.base.ActBase;
import com.tycmc.signelectroniccontracts.tools.images.ImageSetting;

import java.util.List;


/**
 * Created by gy on 2017/11/7.
 */

public class PopQianyue extends PopupWindow {


    private ViewPager viewPager;

    public PopQianyue(final ActBase context, final List<QianYueEntity> list, final QianYueInterface linster) {
        super(context);
        View inflate = ((LayoutInflater) context.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_qianyue, null);
        inflate.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        inflate.findViewById(R.id.bt_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if (linster != null) {
                    linster.queyue(list.get(viewPager.getCurrentItem()));
                }
            }
        });
        viewPager = ((ViewPager) inflate.findViewById(R.id.viewpager));
        viewPager.setOffscreenPageLimit(list.size());
        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }
            @Override
            public View instantiateItem(ViewGroup container, int position){
                ImageView imageView = new ImageView(container.getContext().getApplicationContext());
                View inflate1 = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.item_imageview, container, false);
//                ImageView imageView = (ImageView) inflate1.findViewById(R.id.iv_imageview);
                ImageSetting.urlImage(list.get(position).getAttaPath(), imageView,true);
//                imageView.setLayoutParams(new ViewGroup.LayoutParams(100,100));
//                ImageSetting.urlImage("https://p3.ssl.qhimgs1.com/sdr/400__/t01429f2551b8b0be23.png", imageView,true);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                container.addView(imageView);
                return imageView;
            }
        });
        this.setContentView(inflate);
        // 设置SelectPicPopupWindow弹出窗体的宽
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int screesnWidth = wm.getDefaultDisplay().getWidth();
        int screesnHeight = wm.getDefaultDisplay().getHeight();
        this.setWidth(screesnWidth);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.pop_add_ainm);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    public interface QianYueInterface {
        void queyue(QianYueEntity entity);
    }

    public void show(View view) {
        showAtLocation(view,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}
